/**
 * Copyright (c) 2008 - TheNittyGritty - Thomas Brekelmans
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package nl.thenittygritty.command 
{
	import flash.events.EventDispatcher;	
	import flash.events.ErrorEvent;
	import flash.events.Event;

	import nl.thenittygritty.command.ICommand;
	import nl.thenittygritty.command.ICommandReceiver;	

	/**
	 * AbstractCommand implements ICommand and defines a receiver property which 
	 * is set through it's constructor.
	 * AbstractCommand extends EventDispatcher and enables concrete commands to 
	 * call either onCommandComplete or onCommandError based on the execution of 
	 * their execute logic. Both of these methods then dispatch their
	 * appropriate events (respectively Event.COMPLETE and ErrorEvent.ERROR);
	 */
	public class AbstractCommand extends EventDispatcher implements ICommand 
	{
		private var receiver:ICommandReceiver;

		/**
		 * Creates a new AbstractCommand and assigns a given receiver 
		 * of this command.
		 */
		public function AbstractCommand(receiver:ICommandReceiver) 
		{
			this.receiver = receiver;
		}

		/**
		 * Executes the command.
		 */
		public function execute():void
		{
		}
		
		/**
		 * Dispatches an Event.COMPLETE event which bubbles and is cancelable 
		 * to indicate that this command executed succesfully.
		 */
		protected function onCommandComplete():void 
		{
			dispatchEvent(new Event(Event.COMPLETE, true, true));
		}
		
		/**
		 * Dispatches an ErrorEvent.ERROR event which bubbles and is cancelable 
		 * to indicate that this command encountered an error while executing 
		 * its execute logic.
		 */
		protected function onCommandError(errorMessage:String = null):void 
		{
			dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, true, true, 
						  errorMessage));
		}

		/**
		 * Returns the name of the class in a String.
		 */
		public function toString():String 
		{
			return "AbstractCommand";
		}
	}
}
